package com.appkubes.mall.admin.application.system.announcement.service;

import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.common.utils.data.string.UUIDUtils;
import com.appkubes.mall.admin.application.system.announcement.dao.AnnouncementDao;
import com.appkubes.mall.core.po.MallAnnouncement;
import com.appkubes.mall.core.po.MallAnnouncementExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class AnnouncementService {

    @Autowired
    AnnouncementDao announcementDao;

    public PageInfo querySelective(String name,String appKey, Integer page, Integer size, String sort, String order) {
        MallAnnouncementExample example = new MallAnnouncementExample();
        MallAnnouncementExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, size);
        List<MallAnnouncement> list = announcementDao.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    public MallAnnouncement findById(String announcementKey) {
        return announcementDao.selectByPrimaryKey(announcementKey);
    }

    public int add(MallAnnouncement model) {
        model.setAnnouncementKey(UUIDUtils.getUUID());
        model.setCreateTime(DateUtils.DateToTimestamp(new Date()));
        model.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        model.setAppKey(null);
        return announcementDao.insertSelective(model);
    }

    public int update(MallAnnouncement model) {
        model.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        return announcementDao.updateByPrimaryKeySelective(model);
    }

    public int deleteById(String announcementKey) {
        return announcementDao.deleteByPrimaryKey(announcementKey);
    }

    public int deleteByIds(String[] ids) {
        return announcementDao.delete(ids);
    }

    public int batchenable(String[] ids){
        return announcementDao.batchenable(ids);
    }

    public int batchunenable(String[] ids){
        return announcementDao.batchunenable(ids);
    }


}
